<?php
/**
 * @author wyl
 * @date 2015-10-23 15:08:09
 */
Yii::import('dao.bidAuction.*');
Yii::import('application.components.auctionCache.*');
class AutoBidDao{
	private $_currentAuction;
	private $_currentCustomer;
	private $_currentID;
	private $_amount;
	public $customerId;
	public $auctionId;
	public $autobidId;
	
	public function __construct(){
		$this->_bidDao=new BidAuction();
	}
	
	public function getAmount(){
		return $this->_amount;
	}
	
	private function normalize($result){
		$resultData=array();
		foreach($result as $auction){
		    $resultData[$auction['auction_id']][]=array(
                'autobid_id'=>$auction['id'],		        
		        'customer_id'=>$auction['customer_id'],
		    );
		}
		return $resultData;
	}
	
	public function add($amount,$startPrice=0){
		$sort=AutobidMark::model()->findByPk($this->auctionId.':has');
		$sort=$sort?-1:1;
		$addSql = '
            INSERT INTO
                autobid(auction_id,customer_id,amount,sort,created,init_amount,start_price)
            VALUES (
                "' . $this->auctionId . '",
                "' . $this->customerId . '",
                "' . $amount . '",
                "'.$sort*time().'",
                "' . time() . '",
                "' . $amount . '",
                "' . $startPrice . '" 
            )
        ';
		return Yii::app()->db->createCommand($addSql)->execute();
	}
	
	public function getAutoBidList($auctionID=NULL){
		$sql='
				SELECT
					a.id,a.auction_id,a.customer_id
				FROM
					auction_pool AS p
				LEFT JOIN autobid AS a ON p.auction_id = a.auction_id
				WHERE
					1
				AND p.winner_id <> a.customer_id
				AND p.end_time<='.(time()+15).'
				AND a.amount > 0
				AND p.`status` = 1
				AND p.winner_price>=a.start_price
			';
		$sql=!$auctionID?$sql:$sql.' AND a.auction_id='.$auctionID;
		$sql.=' ORDER BY a.sort ASC';
		$result=Yii::app()->db->createCommand($sql)->queryAll();
		if (!empty($result)) {
			return $this->normalize($result);
		}
		return false;
	}
	public function getAutoBidList1($auctionID=NULL){
		$sql='
				SELECT
					a.id,a.auction_id,a.customer_id
				FROM
					auction_pool AS p
				LEFT JOIN autobid AS a ON p.auction_id = a.auction_id
				WHERE
					1
				AND p.winner_id <> a.customer_id
				AND p.end_time<='.(time()+15).'
				AND a.amount > 0
				AND p.`status` = 1
				AND p.winner_price>=a.start_price
			';
		$sql=!$auctionID?$sql:$sql.' AND a.auction_id='.$auctionID;
		$sql.=' ORDER BY a.sort ASC';
		$result=Yii::app()->db->createCommand($sql)->queryAll();
		if (!empty($result)) {
			return $this->normalize($result);
		}
		return false;
	}

	
	public function subAmount(){
		$sql='
				UPDATE autobid 
				SET 
					amount=@autobid_after_amount:=amount-1 
				WHERE 
					id="'.$this->autobidId.'"	 	
			';
		$executeResult=Yii::app()->db->createCommand($sql)->execute();
		$getAmountSql='
							SELECT @autobid_after_amount AS autobid_after_amount;
					   ';
		$autoBidAmount=Yii::app()->db->createCommand($getAmountSql)->queryRow();
		$this->_amount=$autoBidAmount['autobid_after_amount'];
		return $executeResult;
	}
	
	public function addErrorLog($error){
		$model=new AutobidErrorLog();
		//$error=$this->_bidDao->getErrors();
		$attributes=array(
				'auction_id'	=>$this->auctionId,
				'customer_id'	=>$this->customerId,
				'error_msg'		=>json_encode($error),
				'created'		=>time()
		);
		$model->attributes=$attributes;
		return $model->save();
	}
	
	public function delAutoBid(){
		$autobid=new Autobid();
	    $autobid=$autobid->findByPk($this->autobidId);
	    if (!empty($autobid)){
            $tmpautobid=new AutobidLog();
            $tmpautobid->attributes=$autobid->attributes;
			$tmpautobid->sort = 0;
            $tmpautobid->save();
	    }
		return Autobid::model()->deleteByPk($this->autobidId);
	}
	
	public function delAutoBidOfSomeone(){
		return Autobid::model()->deleteAllByAttributes(array('customer_id'=>$this->customerId));
	}
	
	public function setSort(){
		$model=new Autobid();
		$model=$model->findByPk($this->autobidId);
		if (empty($model)) {
			return FALSE;
		}
		$model->sort=time();
		return $model->save();
	}
	
	
	public function setPrimaryKey($auctionID,$customerID){
		$this->_currentAuction=$auctionID;
		$this->_currentCustomer=$customerID;
		$this->_currentID=$auctionID.':'.$customerID;
		return $this;
	}
	
}